package org.orqlog.dubbo.filter;

import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.rpc.*;
import org.orqlog.Utils.IpUtil;

@Activate(group = {CommonConstants.CONSUMER, CommonConstants.PROVIDER})
public class IpPrint implements Filter {
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        String clientIp = RpcContext.getContext().getAttachment("clientIp");
        if (!StringUtils.isEmpty(clientIp)) {
            IpUtil.setIp(clientIp);
        } else  {
            RpcContext.getContext().setAttachment("clientIp", IpUtil.getIp());
        }
//        System.out.println(">>>IpPrint ip: " + clientIp + " from util: " + IpUtil.getIp());
        return invoker.invoke(invocation);
    }
}
